package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.mapper.DyBlogMapper;
import com.dy.mapper.DyCommentMapper;
import com.dy.pojo.dto.DyCommentDTO;
import com.dy.pojo.entity.DyBlogEntity;
import com.dy.pojo.entity.DyCommentEntity;
import com.dy.pojo.vo.DyCommentVO;
import com.dy.service.DyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/10 11:18
 * @Version 1.0 (版本)
 */
@Service
public class DyCommentServiceImpl extends ServiceImpl<DyCommentMapper, DyCommentEntity> implements DyCommentService {
    @Autowired
    private DyCommentMapper dyCommentMapper;
    @Autowired
    private DyBlogMapper dyBlogMapper;

    @Override
    public List<DyCommentVO> selectAll() {
        List<DyCommentEntity> selectList = dyCommentMapper.selectList(new QueryWrapper<DyCommentEntity>().isNull("parent_code"));
        ArrayList<DyCommentVO> volist = new ArrayList<>();
        for (int i = 0; i <selectList.size() ; i++) {
            List<DyCommentEntity> parentList = dyCommentMapper.selectList(new QueryWrapper<DyCommentEntity>().eq("parent_code", selectList.get(i).getCode()));
            ArrayList<DyCommentVO> voCommentList = new ArrayList<>();
            for (int j = 0; j < parentList.size(); j++) {
                DyCommentVO commentVO = new DyCommentVO();
                commentVO.setNickname(parentList.get(j).getNickname())
                        .setEmail(parentList.get(j).getEmail())
                        .setContent(parentList.get(i).getContent())
                        .setAvatar(parentList.get(i).getAvatar());
                voCommentList.add(commentVO);
            }

            DyCommentVO dyCommentVO = new DyCommentVO();
            dyCommentVO.setNickname(selectList.get(i).getNickname())
                    .setAvatar(selectList.get(i).getAvatar())
                    .setContent(selectList.get(i).getContent())
                    .setEmail(selectList.get(i).getEmail())
                    .setSeed(voCommentList);
            volist.add(dyCommentVO);
        }
        return volist;
    }

    @Override
    public Boolean create(DyCommentDTO dyCommentDTO) {
        List<DyBlogEntity> bcodeList = dyBlogMapper.selectList(new QueryWrapper<DyBlogEntity>().eq("code", dyCommentDTO.getBlogCode()));
        if (bcodeList.size() == 0) {
            return false;
        }
        DyCommentEntity commentEntity = new DyCommentEntity();
        String code="C"+System.currentTimeMillis();
        commentEntity.setAvatar(dyCommentDTO.getAvatar())
                .setBlogCode(dyCommentDTO.getBlogCode())
                .setCode(code)
                .setContent(dyCommentDTO.getContent())
                .setEmail(dyCommentDTO.getEmail())
                .setNickname(dyCommentDTO.getNickname());
        dyCommentMapper.insert(commentEntity);
        return true;
    }

    @Override
    public Boolean reply(DyCommentDTO dyCommentDTO) {
        List<DyCommentEntity> codeList = dyCommentMapper.selectList(new QueryWrapper<DyCommentEntity>().eq("code", dyCommentDTO.getParentCode()));
        if (codeList.size() == 0) {
            return false;
        }
        DyCommentEntity commentEntity = new DyCommentEntity();
        String code="C"+System.currentTimeMillis();
        commentEntity.setAvatar(dyCommentDTO.getAvatar())
                .setCode(code)
                .setParentCode(dyCommentDTO.getParentCode())
                .setContent(dyCommentDTO.getContent())
                .setEmail(dyCommentDTO.getEmail())
                .setNickname(dyCommentDTO.getNickname());
        dyCommentMapper.insert(commentEntity);
        return true;
    }
}
